<#assign tit>收银台</#assign>
<@page.html title=tit>
<script type="text/javascript">
  var _total = ${money!};
  var _alipst = 0;
  var _bspst = 0;
  function doOk() {
    var vv = getpl();
    if(!vv) return;
    if(window.parent && window.parent.payOk)
      window.parent.payOk(vv);
    _alipst *=-1;
    _bspst *= -1;
    cancel();
  }
  function cancel() {
    if(window.parent && window.parent.closeWin)
      window.parent.closeWin();
    else
      window.close();
  }
  function chkClose() {
    if(_bspst>0) {
      window.alert("已经用宝钢积分支付,不能取消付款");
      return false;
    }
    if(_alipst==2) {
      window.alert("已经用支付宝支付,不能取消付款");
      return false;
    }
    if(_alipst==1 && !window.confirm("确定要取消付款吗?")) return false;
    return true;
  }

  function chkp() {
    var tb = getEl("tb_pay");
    if(!tb) return null;
    var mm = 0, m;
    for(var i=0; i<tb.rows.length; i++) {
      tr = tb.rows[i];
      m = tr.cells[1].firstChild.value;
      if(m=="") continue;
      mm += parseFloat(m);
    }
    getEl("sp_change").innerHTML = formatMoney(mm-_total);
  }

  function getpl() {
    var tb = getEl("tb_pay");
    if(!tb) return null;
    var mm = 0, m;
    var tr, arr = new Array();
    var s1 = "wills";
    var s2 = "";
    arr.push(s1+","+s2);
    for(var i=0; i<tb.rows.length; i++) {
      tr = tb.rows[i];
      m = tr.cells[1].firstChild.value;
      if(m=="") continue;
      var pt = tr.getAttribute("pt");
      if(!pt) {
        window.alert("数据错误");
        return null;
      }
      var ss = "";
      if(tr.cells[2].firstChild)
        ss = tr.cells[2].firstChild.value;
      arr.push(pt+","+m+","+ss);
      mm += parseFloat(m);
    }
    if(mm!=_total) {
      window.alert("支付金额不符");
      return null;
    }
    return arr.join("|");
  }

  function gets(c, t) {
    if(c=="") {
      getEl("td_sn"+t).innerHTML = "";
    } else {
      getJson("getSales?code="+encodeURIComponent(c), function(data){
        if(data.ret) {
          getEl("salesCode"+(t>1?t:"")).value = data.data.code;
          getEl("td_sn"+t).innerHTML = data.data.name;
        } else {
          window.alert(data.msg);
          getEl("td_sn"+t).innerHTML = "";
        }
      });
    }
  }


  function alipay(b) {
    var div = getEl("div_alipay");
    if(!div) return;
    div.style.display = b?"":"none";
    div = getEl("div_fullbg");
    if(div) div.style.display = b?"":"none";
    var fm = getEl("fm_alipay");
    fm.barCode.disabled = false;
    fm.money.disabled = false;
    fm.barCode.value = "";
    fm.money.value = formatMoney(_total);
    getEl("bt_pay").style.display = "";
    getEl("bt_payok").style.display = "none";
    getEl("div_alinfo").style.display = "none";
    _aliInfo = null;
    if(b) fm.barCode.focus();
    _alipst = b?1:0;
  }
  function alipaycls() {
    if(_aliInfo) {
      if(!window.confirm("确定要取消付款吗?")) return;
    }
    alipay(false);
  }

  var _aliInfo = null;
  function addAlipay() {
    var fm = getEl("fm_alipay");
    postForm(fm.action, fm, addAlipayRet, function(){
      getEl("bt_pay").disabled = true;
      getEl("div_alinfo").innerHTML = '正在提交支付信息...';
      getEl("div_alinfo").style.display = "";
      return true;
    });
  }
  function addAlipayRet(data) {
    getEl("bt_pay").disabled = false;
    if(data.ret) {
      _aliInfo = data.data;
      var fm = getEl("fm_alipay");
      fm.barCode.disabled = true;
      fm.money.disabled = true;
      getEl("bt_pay").style.display = "none";
      getEl("bt_payok").style.display = "";
      getEl("div_alinfo").innerHTML = '等待用户支付中...';
      getEl("div_alinfo").style.display = "";
      chkAliOk(_aliInfo, false);
    } else {
      window.alert(data.msg);
      getEl("div_alinfo").style.display = "none";
    }
  }

  function alipayOk(b) {
    if(!_aliInfo) return;
    getJson("getAliPay?barCode="+_aliInfo.barCode, function(data){
      if(data.ret) {
        chkAliOk(data.data, b);
      } else {
        //if(b)
          window.alert(data.msg);
        var fm = getEl("fm_alipay");
        fm.barCode.disabled = false;
        fm.money.disabled = false;
        fm.barCode.value = "";
        getEl("bt_pay").style.display = "";
        getEl("bt_payok").style.display = "none";
        getEl("div_alinfo").style.display = "none";
        _aliInfo = null;
        fm.barCode.focus();
      }
    });
  }
  function chkAliOk(ali, b) {
    if(ali.status==1) {
      getEl("ip_ali_money").value = formatMoney(ali.amount);
      getEl("ip_ali_note").value = ali.barCode;
      alipay(false);
      getEl("bt_alip").style.display = "none";
      chkp();
      _alipst = 2;
    } else {
      //if(b)
      //  window.alert("还没有付款成功");
      //else
        window.setTimeout(alipayOk, 3000);
    }
  }



  var _bspInfo;
  function bspay(b) {
    var div = getEl("div_bspay");
    if(!div) return;
    div.style.display = b?"":"none";
    div = getEl("div_fullbg");
    if(div) div.style.display = b?"":"none";
    var fm = getEl("fm_bspay");
    fm.mobile.disabled = false;
    fm.money.disabled = false;
    fm.mobile.value = "";
    fm.money.value = formatMoney(_total);
    fm.smsCode.value = "";
    fm.payPwd.value = "";
    getEl("tr_bs_smscode").style.display = "none";
    getEl("tr_bs_paypwd").style.display = "none";
    getEl("bt_bspay").style.display = "";
    getEl("bt_bspayok").style.display = "none";
    getEl("div_bsinfo").style.display = "none";
    _bspInfo = null;
    if(b) fm.mobile.focus();
  }
  function bspaycls() {
    if(_bspInfo) {
      if(!window.confirm("确定要取消付款吗?")) return;
    }
    bspay(false);
  }

  function addBspay() {
    var fm = getEl("fm_bspay");
    var pmx = window.parent.getBspayParam();
    postForm("_getBsPay?"+pmx, fm, addBspayRet, function(){
      getEl("bt_bspay").disabled = true;
      getEl("div_bsinfo").innerHTML = '正在提交支付信息...';
      getEl("div_bsinfo").style.display = "";
      return true;
    });
  }
  function addBspayRet(data) {
    getEl("bt_bspay").disabled = false;
    if(data.ret) {
      _bspInfo = data.data;
      var fm = getEl("fm_bspay");
      fm.mobile.disabled = true;
      fm.money.disabled = true;
      getEl("tr_bs_smscode").style.display = "";
      getEl("tr_bs_paypwd").style.display = _bspInfo.needPwd?"":"none";
      getEl("bt_bspay").style.display = "none";
      getEl("bt_bspayok").style.display = "";
      getEl("div_bsinfo").style.display = "none";
    } else {
      window.alert(data.msg);
      getEl("div_bsinfo").style.display = "none";
    }
  }

  function bspayOk() {
    if(!_bspInfo) return;
    var fm = getEl("fm_bspay");
    if(fm.smsCode.value=="") {
      window.alert("请输入短信验证码");
      return;
    }
    if(_bspInfo.needPwd && fm.payPwd.value=="") {
      window.alert("请输入支付密码");
      return;
    }
    fm.payId.value = _bspInfo.payId;
    postForm("_getBsPayOk", fm, bspayRet, function(){
      getEl("bt_bspayok").disabled = true;
      getEl("div_bsinfo").innerHTML = '正在提交支付信息...';
      getEl("div_bsinfo").style.display = "";
      return true;
    });
  }
  function bspayRet(data) {
    window.alert(data.msg);
    getEl("bt_bspayok").disabled = false;
    getEl("div_bsinfo").style.display = "none";
    if(data.ret) {
      addbspi(data.data);
    }
  }

  function addbspi(bsi) {
    var tr, tb = getEl("tb_pay");
    if(!tb) return null;
    if(tb.rows[0].cells[1].firstChild.value!="") {
      tr = tb.insertRow(0);
      tr.setAttribute("pt", "S016");
      var th = document.createElement("TH");
      th.innerHTML = "宝钢积分";
      tr.appendChild(th);
      var td = tr.insertCell(-1);
      td.innerHTML = '<input type="text" value="" maxlength="10" size="12" readonly>';
      td = tr.insertCell(-1);
      td.innerHTML = '<input type="hidden" value="" readonly><span></span>';
      _bspst++;
    }
    tr = tb.rows[0];
    tr.cells[1].firstChild.value = formatMoney(bsi.amount);
    tr.cells[2].childNodes[0].value = bsi.payId;
    tr.cells[2].childNodes[1].innerHTML = bsi.mobile;

    chkp();
    bspay(false);
  }

</script>
<div style="">

  <div style="margin-top:8px; padding:5px 0; border:3px solid #d2885b; height:250px; overflow-y:auto;">
    <table id="tb_pay" class="input h1" style="width:100%;">
      <tr pt="S016">
        <th width="100" nowrap>宝钢积分</th>
        <td><input type="text" value="" maxlength="10" size="12" readonly></td>
        <td><input type="hidden" value="" readonly><span></span></td>
      </tr>
      <#if payType??><#list payType as c><#if c.code!='S014' || (giftCard?? && giftCard='1')>
      <tr pt="${c.code!}">
        <th width="100" nowrap>${c.chName!}</th>
        <#if c.code='S011'><#assign alip=1 />
        <td><input type="text" id="ip_ali_money" value="" maxlength="10" size="12" readonly></td>
        <td><input type="text" id="ip_ali_note" value="" maxlength="100" size="20" readonly></td>
        <#else>
        <td><input type="text" value="" onkeyup="chkp();" maxlength="10" size="12"></td>
        <td><#if c.requireNote=1><input type="text" value="" maxlength="100" size="20"></#if></td>
        </#if>
      </tr></#if>
      </#list></#if>
    </table>
  </div>

  <div style="margin-top:8px; padding:10px 12px; border:3px solid #f8a800; background-color:#ffedc5; font-size:18px;">应收金额: <span style="padding-right:20px;">${money?string("0.00")}</span>找零: <span id="sp_change"></span></div>

  <div class="button" style="margin:10px 5px 5px;">
    <input type="button" value="宝钢积分支付" onclick="bspay(true);" style="float:left;">
    <#if alip?? && alipay??><button id="bt_alip" onclick="alipay(true);" style="float:right; height:34px; line-height:28px; font-size:16px; font-family:"微软雅黑";"><img src="${_appPath}/images/alipay.png" height="18" style="padding-bottom:4px;">条码支付</button></#if>
    <input type="button" value="确认提交" onclick="doOk();">
  </div>

</div>


<div id="div_alipay" style="display:none; position:absolute; z-index:1100; left:45px; top:100px; padding:35px 40px; width:320px; border:3px solid #f8a800; background-color:#ffedc5;">
  <a href="javascript:alipaycls();" style="position:absolute; right:10px; top:10px;"><img src="${_appPath}/images/del.png"/></a>
  <form id="fm_alipay" method="post" action="_getAliPay" onsubmit="return false;">
    <div id="div_alinfo" style="display:none; position:absolute; width:320px; height:140px; line-height:120px; text-align:center; background-color:#ffedc5;"></div>
    <table class="input h1" style="width:100%;">
      <tr>
        <th>支付宝条码</th>
        <td><input type="text" name="barCode" value="" req="支付宝条码" maxlength="40" size="20"></td>
      </tr>
      <tr>
        <th>付款金额</th>
        <td><input type="text" name="money" value="" req="付款金额" vtp="number" maxlength="12" size="10"></td>
      </tr>
    </table>
  </form>
  <div class="button" style="margin:20px 10px 5px;">
    <input id="bt_pay" type="button" value="确定" onclick="addAlipay();">
    <input id="bt_payok" style="display:none;" type="button" value="付款完成" onclick="alipayOk(true);"></div>
</div>


<div id="div_bspay" style="display:none; position:absolute; z-index:1100; left:45px; top:50px; padding:35px 40px; width:320px; border:3px solid #f8a800; background-color:#ffedc5;">
  <a href="javascript:bspaycls();" style="position:absolute; right:10px; top:10px;"><img src="${_appPath}/images/del.png"/></a>
  <form id="fm_bspay" method="post" onsubmit="return false;">
    <input type="hidden" name="payId" value="">
    <div id="div_bsinfo" style="display:none; position:absolute; width:320px; height:140px; line-height:120px; text-align:center; background-color:#ffedc5;"></div>
    <table class="input h1" style="width:100%;">
      <tr>
        <th>手机号或卡号</th>
        <td><input type="text" name="mobile" value="" req="手机号或宝钢卡号" maxlength="40" size="20"></td>
      </tr>
      <tr>
        <th>付款金额</th>
        <td><input type="text" name="money" value="" req="付款金额" vtp="number" maxlength="12" size="10"></td>
      </tr>
      <tr id="tr_bs_smscode" style="display:none;">
        <th>短信验证码</th>
        <td><input type="text" name="smsCode" value="" maxlength="8" size="10"></td>
      </tr>
      <tr id="tr_bs_paypwd" style="display:none;">
        <th>支付密码</th>
        <td><input type="password" name="payPwd" value="" maxlength="40" size="10"></td>
      </tr>
    </table>
  </form>
  <div class="button" style="margin:20px 10px 5px;">
    <input id="bt_bspay" type="button" value="确定" onclick="addBspay();">
    <input id="bt_bspayok" style="display:none;" type="button" value="确认付款" onclick="bspayOk();"></div>
</div>

<#if sales??><script type="text/javascript">gets("${sales!}", 1);</script></#if>
</@page.html>